package neu.soft.service.impl;

import neu.soft.mapper.OutOrderMapper;
import neu.soft.service.OutOrderService;
import neu.soft.vo.PageBean;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OutOrderServiceImpl implements OutOrderService {
    @Autowired
    private OutOrderMapper outOrderMapper;

    /**
     * 添加一条出库单
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> addOutOrder(Map<String,Object> map){
        outOrderMapper.addOutOrder(map);
        return ResultBean.success();
    }

    /**
     * 根据所给条件查询出库单
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<PageBean<Map<String,Object>>> queryOutOrder(Map<String,Object> map){
        ResultBean<PageBean<List<Map<String,Object>>>> resultBean = new ResultBean<>();
        PageBean<Map<String,Object>> pageBean = new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String,Object>> oList = outOrderMapper.queryOutOrder(map);
        for(Map<String,Object> oMap:oList){
            Date date = (Date) oMap.get("outTime");
            if(date!=null){
                SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
                String s2 = sdf2.format(date);
                oMap.put("outTime",s2);
            }
        }
        Long outOrderRows = outOrderMapper.getOutOrderRows(map);
        if(oList == null){
            return ResultBean.error(404,"根据所给条件没有出库记录");
        }
        else {
            pageBean.setRowData(oList);
            pageBean.setTotalRows(outOrderRows);
            pageBean.setTotalPages((int) (outOrderRows % pageSize == 0 ? (outOrderRows/pageSize) : (outOrderRows/pageSize + 1)));
            return ResultBean.success(pageBean);
        }
    }


    public ResultBean<List<Map<String, Object>>> queryMingXi(Map<String, Object> map) {
        List<Map<String,Object>>list=outOrderMapper.queryMingXi(map);
        for (int i=0;i<list.size();i++){
            Date date= (Date) list.get(i).get("outTime");
            if(date!=null){
                SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
                String s2 = sdf2.format(date);
                list.get(i).put("outTime",s2);
            }
        }
        return ResultBean.success(list);
    }

    public int existOutOrder(String outOrder){
        return outOrderMapper.existOutOrder(outOrder);
    }

    public List<Map<String,Object>> queryOutOrderById(String outOrderId){
        return outOrderMapper.queryOutOrderById(outOrderId);
    }
}
